package com.meitianhui.convenitenbuy.module.home.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.home.contract.EdinburghWebViewContract;
import com.meitianhui.convenitenbuy.module.home.http.HomeApiDataSource;
import com.meitianhui.convenitenbuy.module.home.model.bean.OfficialStore;
import com.meitianhui.convenitenbuy.module.home.ui.activity.web.EdinburghWebViewActivity;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;

import java.util.HashMap;
import java.util.Map;

public class EdinburghWebViewPresenter implements EdinburghWebViewContract.Presenter {

    private EdinburghWebViewActivity context;
    private EdinburghWebViewContract.View view;

    public EdinburghWebViewPresenter(EdinburghWebViewContract.View view,
                                     EdinburghWebViewActivity context) {
        this.view = view;
        this.context = context;
    }

    @Override
    public void loginHyphenate() {
    }

    @Override
    public void getOfficialStore(String goodsCode, String goodsUrl, String amount,
                                 String dataSourse, String value) {
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("member_type_key", "consumer");
        String json = new Gson().toJson(hashMap);

        HomeApiDataSource.getInstance()
                .getOfficialStore(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "stores.consumer.defaultStore", json)
                .subscribe(new ResponseObserver<BaseResponse<OfficialStore>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<OfficialStore> response) {
                        view.getOfficialStore(response.data, dataSourse, value);
                    }
                });
    }

    @Override
    public void officialStorePost(String storeId, String storeName, String goodsCode,
                                  String goodsUrl, String channelId) {
        Map<String, String> params = new HashMap<>();
        params.put("goods_code", goodsCode);
        params.put("goods_url", goodsUrl);
        params.put("mobile", InfoProvider.getUserInfo(App.getInstance()).getMobile());
        params.put("nick_name", InfoProvider.getConsumerInfo(App.getInstance()).getNick_name());
        params.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        params.put("stores_name", storeName);
        params.put("recommend_stores_id", storeId);
        params.put("channel_id", channelId);
        String json = new Gson().toJson(params);

        HomeApiDataSource.getInstance()
                .officialStorePost(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "order.consumerFreeGetRecordCreate", json)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        view.officialStorePostSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.officialStorePostFail(code, msg);
                    }
                });
    }

    @Override
    public void settingDefaultStore(String storeId) {
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("stores_id", storeId);
        hashMap.put("consumer_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        hashMap.put("is_llm_stores", "Y");
        String json = new Gson().toJson(hashMap);

        HomeApiDataSource.getInstance()
                .settingDefaultStore(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "stores.consumer.favoriteStore", json)
                .subscribe(new ResponseObserver<BaseResponse<OfficialStore>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<OfficialStore> response) {
                        view.settingDefaultStoreSuccess();
                    }
                });
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

}
